#!/usr/bin/perl 

#
# amarokSyndication - Amarok script for syndicating (atom file format) your music to Google Pages
#
# $Id: AtomSyndication.pl 2008-10-05 19:20:32Z dpino$
#
# (C) 2008  Diego Pino  <pinowsky at gmail com>
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
#

package main;

    # Need the absolute path when is lunched from Amarok
use lib "$ENV{'HOME'}/.kde/share/apps/amarok/scripts/atomSyndication/";
use lib "$ENV{'HOME'}/.kde/share/apps/amarok/scripts/atomSyndication/modules/";

use FrmAtomSyndication;
use AtomSyndication;
use Config::General;

    # Read conf vars
if (-e "./.conf.rc") {
    $conf_obj = new Config::General("./.conf.rc");

    %config = $conf_obj->getall;
    $username = $config{"username"};
    $password = $config{"password"};
    $use_firefox = $config{"use_firefox"};
} 

    # Try to log-in
$atomSyndication = new AtomSyndication($username, $password, $use_firefox);
$authtoken = $atomSyndication->getAuthToken();
if ($authtoken ne "") {
    $atomSyndication->run($authtoken);
}

    # Could not log-in, prompt for user data
my $app = Qt::Application(\@ARGV);
my $w = FrmAtomSyndication;
$app->setMainWidget($w);
my $desktop = Qt::DesktopWidget();
my $width = $desktop->width();
my $height = $desktop->height();
$w->move($width/3, $height/3);
$w->resize(300,300);
$w->show;
$app->exec;

    # Window canceled when clicked X
if ($w->getSettings() == undef) {
    exit(0);
}

    # Get settings
($username, $password, $use_firefox) = @{$w->getSettings()};

#--------------------------------------------------
# print "username: $username\n";
# print "password: $password\n";
# print "use_firefox: $use_firefox\n";
#-------------------------------------------------- 

    # Do the work
$atomSyndication = new AtomSyndication($username, $password, $use_firefox);
$authtoken = $atomSyndication->getAuthToken();
if ($authtoken ne "") {
    $atomSyndication->run($authtoken);
} else {
    my $error = Qt::MessageBox(undef, "Error");
    $error->critical($w, "Error", "Could not fetch a valid Google Pages Session ID. Please, check your setttings", 1, 0, 0);
}
